<?php
/**
 * @title 130. 被围绕的区域
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $board
     * @return NULL
     */
    function solve(&$board) {
        /**
         * @since 2023-12-27 行数和列数
         */
        $rows = count($board);
        $cols = count($board[0]);

        /**
         * @since 2023-12-27 所有O元素的坐标集合
         */
        $oArray = [];

        /**
         * @since 2023-12-27 找到所有0的坐标
         */
        for($r=0; $r<$rows; $r++){
            for($c=0; $c<$cols; $c++){
                /**
                 * @since 2023-12-27 找到O
                 */
                if($board[$r][$c] == "O"){
                    $oArray["{$r}-{$c}"] = [$r, $c];
                } else {}
            }
        }

        /**
         * @since 2023-12-27 被包围的O元素用X去覆盖
         */
        while($oArray){
            /**
             * @since 2023-12-27 本轮所有O坐标集合
             */
            $currentOArray = [];

            /**
             * @since 2023-12-27 pop一个O坐标
             */
            $oRowCol = array_pop($oArray);
            $currentOArray[] = $oRowCol;

            /**
             * @since 2023-12-27 四处延伸
             */
            $nodeArray = [$oRowCol];
            while($nodeArray){
                /**
                 * @since 2023-12-27 下次遍历的坐标集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2023-12-27 遍历当前坐标集合
                 */
                foreach ($nodeArray as list($r, $c)){
                    /**
                     * @since 2023-12-27 4个方向移动数组
                     */
                    $moveArray = [
                        [-1,0], // 上
                        [1, 0], // 下
                        [0, -1], // 左
                        [0, 1], // 右
                    ];
                    foreach ($moveArray as list($addR, $addC)){
                        /**
                         * @since 2023-12-27 延伸点坐标
                         */
                        $newR = $r+$addR;
                        $newC = $c+$addC;

                        /**
                         * @since 2023-12-27 延伸点元素是否也是O
                         */
                        $key = "{$newR}-{$newC}";
                        if(isset($oArray[$key])){
                            $currentOArray[] = $oArray[$key];
                            $nextNodeArray[] = $oArray[$key];

                            /**
                             * @since 2023-12-27 移除当前点
                             */
                            unset($oArray[$key]);
                        } else {}

                    }
                }

                /**
                 * @since 2023-12-27 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }

            /**
             * @since 2023-12-27 本轮所有O坐标，是否有到边界
             */
            $arriveSide = false;
            foreach ($currentOArray as list($r, $c)){
                /**
                 * @since 2023-12-27 到达边界
                 */
                if($r == 0 OR $r == $rows-1 OR $c == 0 OR $c == $cols-1){
                    $arriveSide = true;
                    break;
                } else {}
            }

            /**
             * @since 2023-12-27 到达边界，所有0元素不被覆盖，不做任何操作
             */
            if($arriveSide){
            } else {
                /**
                 * @since 2023-12-27 没有到边界，所有0元素被X覆写
                 */
                foreach ($currentOArray as list($r, $c)){
                    $board[$r][$c] = "X";
                }
            }
        }

        /**
         * @return String[][]
         */
        return $board;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_18.php
 */
$datas = [
    [[["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->solve(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
